package com.jspica.app.messagespam;


import android.app.*;
import android.content.*;
import android.content.pm.PackageManager.*;
import android.net.*;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceClickListener;
import android.text.*;
import android.text.method.*;
import android.text.util.*;
import android.util.*;
import android.view.*;
import android.widget.*;

import com.jspica.app.messagespam.R;
import com.jspica.app.messagespam.WhiteList;

public class Preferences extends PreferenceActivity implements OnPreferenceClickListener {

	Context ctx;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ctx = this;

		addPreferencesFromResource(R.xml.settings);
		
		setTitle(getResources().getString(R.string.app_name) + " - " +
				getResources().getString(R.string.settings));
		
		//initMembers();
		//setEventHandlers();
		
		//this.onOptionsItemSelected(item);
		
		/* 각각의 항목을 리스너에 등록 */		
		findPreference("show_prog_info").setOnPreferenceClickListener(this);
		findPreference("show_whitelist").setOnPreferenceClickListener(this);
		findPreference("show_blacklist").setOnPreferenceClickListener(this);
		findPreference("show_help").setOnPreferenceClickListener(this);
	}

	protected void onResume() {
		super.onResume();
	}
	
	/* 항목 클릭했을때 이벤트 처리 */
	@Override
	public boolean onPreferenceClick(Preference preference) { 
		String version = "";
		try {
			version = getPackageManager().
				getPackageInfo(getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {}
		
		
		// 경고창의 텍스트를 자동 링크걸기 위해서
		TextView message = new TextView(ctx);
		SpannableString s = new SpannableString(getText(R.string.prog_text));
		Linkify.addLinks(s, Linkify.WEB_URLS);
		message.setText(s);
		message.setMovementMethod(LinkMovementMethod.getInstance());
		message.setTextSize(0, 23);
		message.setPadding(20, 20, 20, 20);
		message.setLineSpacing(20, 1);
	
		
		if (preference.getKey().equals("show_prog_info")) {
			new AlertDialog.Builder(this)
			.setTitle(getResources().getString(R.string.app_name) + "\nversion : " + version)
			//.setMessage(getResources().getString(R.string.prog_text))
			.setView(message)
			.setIcon(R.drawable.icon)
			.setCancelable(false) // Back버튼 작동안하도록.
			.setNegativeButton(getResources().getString(R.string.close), null)
			.show();

			return true;
			
		} else if (preference.getKey().equals("show_whitelist")) {
			Log.e("@@@@@@@@","show_wihteswlist");
			try {
				Intent intent = new Intent(this, WhiteList.class);
				startActivity(intent);
			} catch (ActivityNotFoundException e) {
			}
		
			return true;
			
		} else if (preference.getKey().equals("show_blacklist")) {
			Log.e("@@@@@@@@","show_blacklist");
			try {
				Intent intent = new Intent(this, BlackList.class);
				startActivity(intent);
			} catch (ActivityNotFoundException e) {
			}
		
			return true;
	
		} else if (preference.getKey().equals("show_help")) {
			Log.e("@@@@@@@@","show_help");
			Intent intent = new Intent(Intent.ACTION_VIEW,
					Uri.parse("http://jspica.wordpress.com/2011/08/22/explanation"));
			startActivity(intent);
	
			return true;
		}		
		
		
		return false;		
	}
}
